package initialize

import (
	"encoding/json"

	"github.com/astaxie/beego/logs"
)

type Logs struct{}

// convert string to log-type
func (L Logs) convertLogLevel(level string) int {

	switch level {
	case "warn":
		return logs.LevelWarn
	case "info":
		return logs.LevelInfo
	case "trace":
		return logs.LevelTrace
	default:
		return logs.LevelDebug
	}

}

// init log cfg
func (L Logs) Init(option Option) {

	config := make(map[string]interface{})

	config["filename"] = option.LogsOption.LogPath
	config["level"] = L.convertLogLevel(option.LogsOption.LogLevel)

	configStr, err := json.Marshal(config)
	if err != nil {
		logs.Debug("[log] init logger failed", err)
		return
	}
	logs.SetLogger(logs.AdapterFile, string(configStr))
}
